﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using Dictionary.ERP;
using Common;
using Common.Class;
using Extra.ERP;
using Security.ERP;
using System.Data;

namespace Dictionary
{
    public class xucHospitalAdd : xucBaseAdd
    {
        public delegate void SuccessEventHander(object sender, DIC_HOSPITAL item);

        private readonly IContainer components = null;
        private CheckEdit chxUse;
        private GridLookUpEdit glkProvince;
        private GridColumn gridColumn3;
        private GridColumn gridColumn4;
        private GridView gridLookUpEdit1View;
        private LabelControl lcDescription;
        private LabelControl lcProvince;
        private TextEdit txtDescription;

        public xucHospitalAdd()
        {
            InitializeComponent();
            InitData();
        }

        public event SuccessEventHander Success;

        protected override void Add()
        {
            base.Add();
            base.txtID.Text = new DIC_HOSPITAL().NewID();
            base.txtNAME.Focus();
        }

        public void Clear()
        {
            base.txtID.Text = "";
            base.txtNAME.Text = "";
            txtDescription.Text = "";
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        protected override void Init()
        {
        }

        private void InitData()
        {
            DataTable list = new DIC_PROVINCE().GetList();
            glkProvince.Properties.DataSource = list;
        }

        private void InitializeComponent()
        {
            var manager = new ComponentResourceManager(typeof (xucHospitalAdd));
            chxUse = new CheckEdit();
            txtDescription = new TextEdit();
            lcDescription = new LabelControl();
            glkProvince = new GridLookUpEdit();
            gridLookUpEdit1View = new GridView();
            gridColumn3 = new GridColumn();
            gridColumn4 = new GridColumn();
            lcProvince = new LabelControl();
            base.gcInfo.BeginInit();
            base.gcInfo.SuspendLayout();
            base.txtNAME.Properties.BeginInit();
            base.txtID.Properties.BeginInit();
            base.imageCollection2.BeginInit();
            ((ISupportInitialize) base.Err).BeginInit();
            chxUse.Properties.BeginInit();
            txtDescription.Properties.BeginInit();
            glkProvince.Properties.BeginInit();
            gridLookUpEdit1View.BeginInit();
            base.SuspendLayout();
            base.gcInfo.Controls.Add(glkProvince);
            base.gcInfo.Controls.Add(lcProvince);
            base.gcInfo.Controls.Add(chxUse);
            base.gcInfo.Controls.Add(txtDescription);
            base.gcInfo.Controls.Add(lcDescription);
            base.gcInfo.Dock = DockStyle.Top;
            base.gcInfo.Location = new Point(0, 0);
            base.gcInfo.Size = new Size(0x14d, 0x99);
            base.gcInfo.Controls.SetChildIndex(base.lblName, 0);
            base.gcInfo.Controls.SetChildIndex(base.txtNAME, 0);
            base.gcInfo.Controls.SetChildIndex(base.lblId, 0);
            base.gcInfo.Controls.SetChildIndex(base.txtID, 0);
            base.gcInfo.Controls.SetChildIndex(lcDescription, 0);
            base.gcInfo.Controls.SetChildIndex(txtDescription, 0);
            base.gcInfo.Controls.SetChildIndex(chxUse, 0);
            base.gcInfo.Controls.SetChildIndex(lcProvince, 0);
            base.gcInfo.Controls.SetChildIndex(glkProvince, 0);
            base.txtNAME.Location = new Point(0x3f, 0x34);
            base.txtNAME.Size = new Size(0xfd, 20);
            base.lblName.Location = new Point(3, 0x37);
            base.txtID.Location = new Point(0x3f, 0x1a);
            base.txtID.Size = new Size(0xfd, 20);
            base.imageCollection2.ImageStream =
                (ImageCollectionStreamer) manager.GetObject("imageCollection2.ImageStream");
            base.btnSave.Location = new Point(0x27, 0xa2);
            base.btnCancel.Location = new Point(230, 0xa2);
            base.btnSaveNew.Location = new Point(0x89, 0xa2);
            chxUse.EditValue = true;
            chxUse.Location = new Point(60, 0x80);
            chxUse.Name = "chxUse";
            chxUse.Properties.Caption = "C\x00f2n quản l\x00fd";
            chxUse.Size = new Size(0x61, 0x12);
            chxUse.TabIndex = 9;
            txtDescription.Location = new Point(0x3f, 0x65);
            txtDescription.Name = "txtDescription";
            txtDescription.Size = new Size(0xfd, 20);
            txtDescription.TabIndex = 7;
            lcDescription.Location = new Point(4, 0x68);
            lcDescription.Name = "lcDescription";
            lcDescription.Size = new Size(0x27, 13);
            lcDescription.TabIndex = 8;
            lcDescription.Text = "Ghi ch\x00fa:";
            glkProvince.Location = new Point(0x3f, 0x4b);
            glkProvince.Name = "glkProvince";
            glkProvince.Properties.Buttons.AddRange(new[]
            {new EditorButton(ButtonPredefines.Combo), new EditorButton(ButtonPredefines.Plus)});
            glkProvince.Properties.DisplayMember = "ProvinceName";
            glkProvince.Properties.NullText = "[Chọn tỉnh]";
            glkProvince.Properties.ValueMember = "ProvinceCode";
            glkProvince.Properties.View = gridLookUpEdit1View;
            glkProvince.Size = new Size(0xfd, 20);
            glkProvince.TabIndex = 0x20;
            gridLookUpEdit1View.Columns.AddRange(new[] {gridColumn3, gridColumn4});
            gridLookUpEdit1View.FocusRectStyle = DrawFocusRectStyle.RowFocus;
            gridLookUpEdit1View.Name = "gridLookUpEdit1View";
            gridLookUpEdit1View.OptionsSelection.EnableAppearanceFocusedCell = false;
            gridLookUpEdit1View.OptionsView.ShowGroupPanel = false;
            gridColumn3.Caption = "M\x00e3 chi nh\x00e1nh";
            gridColumn3.FieldName = "ProvinceCode";
            gridColumn3.Name = "gridColumn3";
            gridColumn3.Visible = true;
            gridColumn3.VisibleIndex = 0;
            gridColumn4.Caption = "T\x00ean chi nh\x00e1nh";
            gridColumn4.FieldName = "ProvinceName";
            gridColumn4.Name = "gridColumn4";
            gridColumn4.Visible = true;
            gridColumn4.VisibleIndex = 1;
            lcProvince.Location = new Point(4, 0x4e);
            lcProvince.Name = "lcProvince";
            lcProvince.Size = new Size(0x36, 13);
            lcProvince.TabIndex = 0x1f;
            lcProvince.Text = "Thuộc tỉnh:";
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Name = "xucHospitalAdd";
            base.Size = new Size(0x14d, 0xc5);
            base.gcInfo.EndInit();
            base.gcInfo.ResumeLayout(false);
            base.gcInfo.PerformLayout();
            base.txtNAME.Properties.EndInit();
            base.txtID.Properties.EndInit();
            base.imageCollection2.EndInit();
            ((ISupportInitialize) base.Err).EndInit();
            chxUse.Properties.EndInit();
            txtDescription.Properties.EndInit();
            glkProvince.Properties.EndInit();
            gridLookUpEdit1View.EndInit();
            base.ResumeLayout(false);
        }

        private void RaiseSuccessEventHander(DIC_HOSPITAL item)
        {
            if (Success != null)
            {
                Success(this, item);
            }
        }

        public void SetData(DIC_HOSPITAL item)
        {
            base.txtID.Text = item.HospitalCode;
            SYS_LOG.Insert("Danh Mục Bằng Cấp", "Xem", base.txtID.Text);
            if (base.m_Status == Actions.Update)
            {
                base.txtID.Properties.ReadOnly = true;
            }
            base.txtNAME.Text = item.HospitalName;
            glkProvince.EditValue = item.ProvinceCode;
            txtDescription.Text = item.Description;
            chxUse.Checked = item.Active;
        }

        protected override void txtID_EditValueChanged(object sender, EventArgs e)
        {
            var control = (TextEdit) sender;
            if (control.ErrorText != string.Empty)
            {
                base.Err.SetError(control, string.Empty);
            }
            if (base.m_Status == Actions.Add)
            {
                var dic_hospital = new DIC_HOSPITAL();
                if (dic_hospital.Exist(control.Text))
                {
                    base.Err.SetError(control, "M\x00e3 đ\x00e3 tồn tại.");
                    control.Focus();
                }
            }
        }

        protected override void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            var control = (TextEdit) sender;
            if ((e.KeyCode == Keys.Enter) | (e.KeyCode == Keys.Tab))
            {
                var dic_hospital = new DIC_HOSPITAL();
                if ((base.m_Status == Actions.Add) && dic_hospital.Exist(control.Text))
                {
                    base.Err.SetError(control, "M\x00e3 đ\x00e3 tồn tại.");
                    control.Focus();
                }
            }
        }

        protected override string uc_Change()
        {
            return string.Empty;
        }

        protected override string uc_Delete()
        {
            var item = new DIC_HOSPITAL
            {
                HospitalCode = base.txtID.Text
            };
            string str = item.Delete();
            if (str == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            return str;
        }

        protected override string uc_Save()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiHospital") != "OK")
            {
                return "";
            }
            if (!MyRule.AllowAdd)
            {
                MyRule.Notify();
                return "";
            }
            if (glkProvince.EditValue == null)
            {
                XtraMessageBox.Show("Vui l\x00f2ng chọn tỉnh th\x00e0nh phố!", "Th\x00f4ng B\x00e1o",
                    MessageBoxButtons.OK);
                return "";
            }
            SYS_LOG.Insert("Danh Mục Bằng Cấp", "Th\x00eam", base.txtID.Text);
            base.SetWaitDialogCaption("Đang lưu dữ liệu...");
            Cursor.Current = Cursors.WaitCursor;
            var item = new DIC_HOSPITAL(base.txtID.Text, glkProvince.EditValue.ToString(), base.txtNAME.Text,
                txtDescription.Text, chxUse.Checked);
            string text = item.Insert();
            if (text == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            Cursor.Current = Cursors.Default;
            this.DoHide();
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            return text;
        }

        protected override string uc_Update()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiHospital") != "OK")
            {
                return "";
            }
            if (!MyRule.AllowEdit)
            {
                MyRule.Notify();
                return "";
            }
            if (glkProvince.EditValue == null)
            {
                XtraMessageBox.Show("Vui l\x00f2ng chọn tỉnh th\x00e0nh phố!", "Th\x00f4ng B\x00e1o",
                    MessageBoxButtons.OK);
                return "";
            }
            SYS_LOG.Insert("Danh Mục Bằng Cấp", "Cập Nhật", base.txtID.Text);
            base.SetWaitDialogCaption("Đang cập nhật dữ liệu...");
            var item = new DIC_HOSPITAL(base.txtID.Text, glkProvince.EditValue.ToString(), base.txtNAME.Text,
                txtDescription.Text, chxUse.Checked);
            string text = item.Update();
            if (text == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            this.DoHide();
            return text;
        }
    }
}